package com.jewaton.auth.commons.operationlog.action;

import com.jewaton.auth.commons.action.BaseAdminAction;
import com.jewaton.auth.commons.operationlog.aspect.OperateLogAnnotation;
import com.jewaton.auth.commons.operationlog.entity.Operatelog;
import com.jewaton.auth.commons.operationlog.service.OperatelogService;
import com.jewaton.auth.commons.shiro.RBACConstants;
import com.jewaton.auth.commons.util.Constants;
import com.jewaton.auth.commons.util.PagedQueryResult;
import com.jewaton.auth.commons.util.Pager;
import com.jewaton.auth.commons.util.StringUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 日志记录列表ACTION
 */
public class OperatelogAction extends BaseAdminAction {
	
	@Resource(name = "operatelogService")
	private OperatelogService operatelogService;

	private Operatelog operatelog;
	private Pager page;
	
	private PagedQueryResult result;
	private Integer currentPage = 1;
	private Integer total;
	private List<Operatelog> pageList;
	
	public String findOperatelogAll(){
		Integer start = 1;
		Integer end = 10;
		page =  operatelogService.findOperatelogAll(start, end);
		return SUCCESS;
	}

	
	@SuppressWarnings("rawtypes")
	@OperateLogAnnotation(moduleName="系统管理",operateTypeName="日志记录查询",message="系统管理-日志记录查询",httpMethod="ALL")
	public String findOperatelogPage(){
		Map<String,Object> param = new HashMap<String,Object>();
		String keywords = request.getParameter("keywords");
		String startTime = request.getParameter("startTime");
		String endTime = request.getParameter("endTime");
		String district = request.getParameter("district");
		String export = StringUtil.trimToEmpty(request.getParameter("export"));
		request.setAttribute("keywords", keywords);
		request.setAttribute("startTime",startTime );
		request.setAttribute("endTime",endTime );
		request.setAttribute("district",district);
		param.put("keywords", keywords);
		if(StringUtils.isNotBlank(startTime)){
			param.put("startTime", startTime + " 0:00:00" );
		}
		if(StringUtils.isNotBlank(endTime)){
			param.put("endTime", endTime + " 23:59:59" );
		}
		param.put("district", district);
		if("yes".equals(StringUtil.trimToEmpty(export))){
			response.setHeader("Content-disposition", "attachment;filename=Operatelog.xls");
			List list = operatelogService.finOperaterLogPager(param, -1,-1).getList();
			request.setAttribute("list", list);
			return "download";
		}else{
			pager = operatelogService.finOperaterLogPager(param, pager.getPageNo(),pager.getPageSize());
			return SUCCESS;
		}
	}

    @Transactional
    @OperateLogAnnotation(moduleName="系统管理",operateTypeName="日志记录删除",message="系统管理-删除日志记录",httpMethod="ALL")
    public String deleteLogs(){
        if(!SecurityUtils.getSubject().hasRole(RBACConstants.PC.CITY_AUDITOR_ROLE))
            return "unauthorized" ;
        String fns = StringUtil.trimToEmpty(request.getParameter("fns"));
        String startTime = StringUtil.trimToEmpty(request.getParameter("startTime"));
        String endTime = StringUtil.trimToEmpty(request.getParameter("endTime"));
        int count = operatelogService.delLogs(fns, startTime, endTime, getSessionPCAaccount());
        if(count>0) request.getSession().setAttribute(Constants.SESSION_DO_FEEDBACK, "删除成功");
        return SUCCESS;
    }


	
	public void setOperatelogService(OperatelogService operatelogService) {
		this.operatelogService = operatelogService;
	}



	public Operatelog getOperatelog() {
		return operatelog;
	}

	public void setOperatelog(Operatelog operatelog) {
		this.operatelog = operatelog;
	}

	public Pager getPage() {
		return page;
	}

	public void setPage(Pager page) {
		this.page = page;
	}

	public Integer getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(Integer currentPage) {
		this.currentPage = currentPage;
	}

	public Integer getTotal() {
		return total;
	}

	public void setTotal(Integer total) {
		this.total = total;
	}

	public List<Operatelog> getPageList() {
		return pageList;
	}

	public void setPageList(List<Operatelog> pageList) {
		this.pageList = pageList;
	}

}
